<?php

/* load other admin code segments */
include('cable_admin_0.php');
include('cable_admin_1.php');
include('cable_admin_2.php');
include('cable_admin_3.php');
include('cable_admin_4.php');

function cable_admin_load($connector)
{
	/* DEPENDENCIES */
		
	/* check for admin segments */
	$ok = true;
	if(!is_readable('cable_admin_0.php'))
		$ok = 'cable_admin_0.php';
	if(!is_readable('cable_admin_1.php'))
		$ok = 'cable_admin_1.php';
	if(!is_readable('cable_admin_2.php'))
		$ok = 'cable_admin_2.php';
	if(!is_readable('cable_admin_3.php'))
		$ok = 'cable_admin_3.php';
	if(!is_readable('cable_admin_4.php'))
		$ok = 'cable_admin_4.php';
	if ($ok !== true)
		return "Could not find admin file \"$ok\"";
	
	return true;
}

function cable_admin_unload($connector)
{
	return true;
}

function cable_admin(&$connector)
{
	global $cable_admin_plugin;
	
	if (($ret = CABLE_adminInit($connector)) !== true)
		return $ret;

	/* decide what to do with the action */
	switch (urldecode($_REQUEST['action']))
	{
		case 'login':
			$ret = CABLE_adminLogin();
			break;
		case 'logout':
			$ret = CABLE_adminLogout();
			break;
		case 'sign_up':
			$ret = CABLE_adminSignup();
			break;
		case 'new_entry':
			$ret = CABLE_adminEntryNew();
			break;
		case 'delete_entry':
			$ret = CABLE_adminEntryDelete();
			break;
		case 'edit_entry':
			$ret = CABLE_adminEntryEdit();
			break;
		case 'list_entries':
			$ret = CABLE_adminEntryList();
			break;
		case 'edit_plugins':
			$ret = CABLE_adminPlugins();
			break;
		case 'edit_themes':
			$ret = CABLE_adminThemes();
			break;
		case 'edit_account':
		case 'ban_user':
			$ret = CABLE_adminAccount();
			break;
		case 'list_users':
			$ret = CABLE_adminUsersList();
			break;
		case 'list_files':
			$ret = CABLE_adminFileList();
			break;
		case 'upload_files':
			$ret = CABLE_adminFileUpload();
			break;
		case 'clear_caches':
			$ret = CABLE_adminClearCaches();
			break;
		case 'general_settings':
			$ret = CABLE_adminPreferences();
			break;
		case 'lost_password':
			$ret = CABLE_adminLost();
			break;
		default:
			if (($cable_admin_plugin[action_template] = $cable_admin_plugin[action_template]->GetChild('CABLE-DefaultPage')) === false)
				return 'Error retrieving default admin page from cable_admin.admin_4 template';
			break;
	}
	
	/* check validity of the function action's response */
	if ($ret !== true)
		return $ret;
	
	/* insert the menu template */
	if (($ret = CABLE_adminMenu($connector)) !== true)
		return $ret;
	$connector->template->ReplaceInsert('CABLE-Menu',$cable_admin_plugin[menu_template]->template);
	
	/* insert the action template */
	if ($cable_admin_plugin[action_template]->template)
		$connector->template->ReplaceInsert('CABLE-Action',$cable_admin_plugin[action_template]->template);
		
	/* other replacement stuff */
	$connector->template->ReplaceInsert('CABLE-Site.name',$connector->config[SiteName]);
	$connector->template->ReplaceInsert('CABLE-Base.url',$connector->config[SiteURL]);
	$connector->template->ReplaceInsert('CABLE-Files.url',$connector->config[SiteURL].'files/'.$connector->config[ThemeName].'/');
	$connector->template->ReplaceInsert('CABLE-CSS.url',$connector->config[SiteURL].'css/'.$connector->config[ThemeName].'/');
	
	/* set user info */
	$connector->template->SetBool('CABLE-User.loggedin',$connector->user->logged_in);
	$connector->template->SetBool('CABLE-User.isadmin',$connector->user->is_admin);
	$permissible = array('username','fullname','firstname','lastname');
	foreach($connector->user->values as $f => $v)
		if (in_array($f,$permissible))
			$connector->template->ReplaceInsert('CABLE-User.'.$f,$v);
			
	return true;
}